package com.jf.cloud.biz.controller.admin;


import cn.hutool.core.collection.CollUtil;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.jf.cloud.biz.constant.NotifyType;
import com.jf.cloud.biz.dto.NotifyLogDTO;
import com.jf.cloud.biz.service.NotifyLogService;
import com.jf.cloud.biz.vo.NotifyLogVO;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.constant.StatusEnum;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 *
 *
 * @author zz
 * @date 2020-05-12 08:21:24
 */
@RestController("adminNotifyLogController")
@RequestMapping("/mp/notify_log")
@Tag(name = "商家端和供应商端消息通知")
public class NotifyLogController {

    @Autowired
    private NotifyLogService notifyLogService;


    /**
     * 查询用户未读消息数量
     */
    @GetMapping("/unread_count" )
    @Operation(summary = "查询用户未读消息数量" , description = "查询用户未读消息数量")
    public ServerResponseEntity<Integer> getNotifyCount() {
        Long tenantId = AuthUserContext.get().getTenantId();
        return ServerResponseEntity.success(notifyLogService.countUnreadBySendTypeAndRemindType(tenantId, Constant.MSG_TYPE));
    }


    /**
     * 查询未读消息列表
     */
    @GetMapping("/unread_count_list")
    @Operation(summary = "查询消息列表" , description = "查询消息列表")
    @Parameter(name = "status", description = "状态 0未读 1已读 不传查询全部按照已读未读排序" )
    public ServerResponseEntity<PageVO<NotifyLogVO>> getUnReadCountList(@Valid PageDTO page, @ParameterObject NotifyLogDTO notifyLogDTO) {
        Long tenantId = AuthUserContext.get().getTenantId();
        notifyLogDTO.setShopId(tenantId);
        notifyLogDTO.setSysType(AuthUserContext.get().getSysType());
        notifyLogDTO.setRemindType(NotifyType.APP.value());
//        status = Objects.isNull(status) ? StatusEnum.DISABLE.value() : status;
        PageVO<NotifyLogVO> notifyLogPageVO = notifyLogService.pageShopNotifyLogByParam(page,notifyLogDTO);
        List<NotifyLogVO> records = notifyLogPageVO.getList();
        List<NotifyLogVO> updateList = new ArrayList<>();
        for (NotifyLogVO record : records) {
            if (!Objects.equals(StatusEnum.DISABLE.value(), record.getStatus())) {
                continue;
            }
            NotifyLogVO notifyLogVO = new NotifyLogVO();
            notifyLogVO.setLogId(record.getLogId());
            notifyLogVO.setStatus(StatusEnum.ENABLE.value());
            updateList.add(notifyLogVO);
        }
        if(CollectionUtils.isNotEmpty(updateList)) {
            notifyLogService.updateBatchById(updateList);
        }
        return ServerResponseEntity.success(notifyLogPageVO);
    }

    @GetMapping("/info")
    @Operation(summary = "获取消息详情" , description = "根据id获取消息详情")
    public ServerResponseEntity<NotifyLogVO> getById(@RequestParam Long logId) {
        notifyLogService.updateStatus(logId);
        NotifyLogVO notifyLogVO = notifyLogService.getByLogId(logId);
        if (Objects.isNull(notifyLogVO)) {
            throw new LuckException(ResponseEnum.DATA_ERROR);
        }
        return ServerResponseEntity.success(notifyLogVO);
    }

    @PutMapping("/is_read")
    @Operation(summary = "批量设置消息为已读状态 " , description = "批量设置消息为已读状态")
    @Parameters(value = {
            @Parameter(name = "logIds", description = "消息id" , required = true),
            @Parameter(name = "type", description = "数据类型 1全部 0已选")
    })
    public ServerResponseEntity<Void> isRead( @RequestParam(value="logIds", required = false) List<Long> logIds,  Integer type) {
        Long tenantId = AuthUserContext.get().getTenantId();
        if(Objects.equals(type, 1)){
            notifyLogService.isReadByShopId(tenantId);
        }
        else if(Objects.equals(type, 0) && CollUtil.isNotEmpty(logIds)){
            notifyLogService.isReadByIds(logIds);
        }
        return ServerResponseEntity.success();
    }


}

